//
//  HelpVc.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/1/3.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class HelpVc: UIViewController,UITextFieldDelegate {
    lazy var textField:UITextField = {
        let text = UITextField.init(frame: FRAME_CGFloat(17.5, 0, SCREEN_WIDTH - 35, 50))
        text.placeholder = "请提出宝贵意见，我们将不断改善"
        text.font = FONT(13)
        let imageView = UIView.init(frame: FRAME_CGFloat(-17.5, 50, SCREEN_WIDTH, 1))
        imageView.backgroundColor = RGB_Same(240, 1)
        text.addSubview(imageView)
        return text
    }()
    
    lazy var phoneField:UITextField = {
        let text = UITextField.init(frame: FRAME_CGFloat(17.5, 50, SCREEN_WIDTH - 35, 50))
        text.placeholder = "您的手机号（必填）"
        text.font = FONT(13)
        let imageView = UIView.init(frame: FRAME_CGFloat(-17.5, 50, SCREEN_WIDTH, 1))
        imageView.backgroundColor = RGB_Same(240, 1)
        text.addSubview(imageView)
        return text
    }()
    
    lazy var presentBtn:UIButton = {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(37.5, 200, SCREEN_WIDTH - 75, 44)
        button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        button.setBackgroundImage(IMAGE(name: "Rectangle"), for: .normal)
        button.setTitle("提交", for: .normal)
        button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 18)
        button.setTitleColor(WhiteBackColor, for: .normal)
        button.layer.cornerRadius = 3
        button.tag = 11
        return button
    }()
    
    lazy var serviceBtn:UIButton = {
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(0, 390, 130, 30)
        button.addTarget(self, action: #selector(btnAction(btn:)), for: .touchUpInside)
        button.setImage(IMAGE(name: "Mine_service"), for: .normal)
        button.layer.cornerRadius = 3
        button.tag = 12
        button.isHidden = true
        return button
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.navigationItem.title = "意见与反馈"

        self.view.addSubview(textField)
        self.view.addSubview(phoneField)
        self.view.addSubview(serviceBtn)
        self.view.addSubview(presentBtn)
        
        self.phoneField.addTarget(self, action: #selector(textFieldDidChange), for: UIControl.Event.editingChanged)
        self.textField.addTarget(self, action: #selector(textFieldDidChange), for: UIControl.Event.editingChanged)
        setLoginBtnStatue(isSelected: false)
        
        SET_LINEVIEW(view: self.view)
    }
    
    @objc func btnAction(btn: UIButton) {
        if btn.tag == 11{
            self.textField.resignFirstResponder()
            self.phoneField.resignFirstResponder()
            postData()
            
        } else if btn.tag == 12 {

        }
    }
    
    @objc fileprivate func textFieldDidChange() {
        self.phoneField.text = limitInput(digit: 11, input: phoneField.text!)
        if checkInput() {
            setLoginBtnStatue(isSelected: true)
        } else {
            setLoginBtnStatue(isSelected: false)
        }
    }
    
    fileprivate func setLoginBtnStatue(isSelected: Bool) {
        if isSelected {
            self.presentBtn.setTitleColor(WhiteBackColor, for: UIControl.State.normal)
            self.presentBtn.isUserInteractionEnabled = true
        } else {
            self.presentBtn.setTitleColor(WhiteBackColor, for: UIControl.State.normal)
            self.presentBtn.isUserInteractionEnabled = false
        }
    }
    
    fileprivate func checkInput() -> Bool {
        if !validMobile(mobile: self.phoneField.text!) {
            return false
        }
        if self.textField.text!.trim(false) == "" {
            return false
        }
        return true
    }
    
    func postData() {
        let para = ["userId":String.getUserId(),"adviseContent":self.textField.text,"mobile":self.phoneField.text] as [String : Any]
        MineRequestModel.feekBack(parameters: para) { (success,message) in
            TTAlertView.showMessage(message: message)
        }
    }
}
